<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@include file="../commons/commons.jsp"%>
<!DOCTYPE html>
	<head>
		<meta charset="utf-8" />
		<title>后台</title>
		<script src="${ctx }/js/city.js"></script>
		<script src="${ctx }/static/js/crud.js"></script>
	</head>

	<body>
	<form action="${ctx }/site/list.do" id="submit-form">
	<div class="page-body">
		<div class="row">
			<div class="col-xs-12 col-md-12">
				<div class="widget">
					<div class="widget-header ">
						<span class="widget-caption">地域站点（省）管理</span>
					</div>
					<div class="table-toolbar">
						<select id="province" runat="server" onchange="selectprovince(this);" style=" width:95px;"></select>
						<select id="city" runat="server" style=" width:95px;"></select>
						<a id="editabledatatable_new" href="javascript:void(0)" class="btn btn-default" onclick="save()">添加站点</a>
					</div>
						<table class="table table-striped table-bordered table-hover"
							id="simpledatatable" style="text-align:center;">
							<thead>
								<tr>
									<th>序号</th>
									<th>省份</th>
									<th>操作</th>
								</tr>
							</thead>
							<tbody>
								 <c:forEach items="${pagination.rows }" var="item" varStatus="status">
                                      <tr>
                                          <td>${status.count}</td>
                                          <td>${item.province}</td>
                                      	  <td>
                                       	     <a href="${ctx }/site/citylist.do?siteProvinceid=${item.id}" class="btn btn-info btn-xs edit" onclick=""><i class="fa fa-edit"></i>进入</a>
                                          	 <a href="javascript:void(0)" class="btn btn-danger btn-xs delete" onclick="del('${item.id}')"><i class="fa fa-trash-o"></i>删除</a>
                                          </td>
                                      </tr>
                                </c:forEach>
							</tbody>
						</table>
						<%@include file="../commons/page.jsp"%>
					</div>
				</div>
			</div>
		</div>
	</form>
</body>
<script type="text/javascript">
	function del(id){
			bootbox.confirm("你确定删除吗?该操作会删除站点下的所有站点信息", function(result) {
				if(result){
					var url = '${ctx }' + "/site/provincedel.do";
				var sendData = {
					"provincesiteid" : id
				};
				$.get(url, sendData, function(backData) {
					if (backData.status == 1) {
						window.location.href="${ctx }/site/list.do";
					} else {
						warningAlert("删除操作出现错误，暂时无法删除");
					}
				});
				}
			});
        }
       
       function save(id){
			bootbox.confirm("你确定添加该站点吗", function(result) {
				if(result){
					var url = '${ctx }' + "/site/save.do";
				var sendData = {
					"provincesite" : $("#province").val(),
					"citysite" : $("#city").val()
				};
				$.post(url, sendData, function(backData) {
					if (backData.status == 1) {
						window.location.href="${ctx }/site/list.do";
					} else {
						warningAlert(backData.message[0].msg);
					}
				});
				}
			});
        }

    var ddlProvince = document.getElementById("province");
    var ddlCity = document.getElementById("city");
    var firstprovince = list2[0];
    for(var i =0;i<list1.length; i++)
    {
        var option = document.createElement("option");
        option.appendChild(document.createTextNode(list1[i]));
        option.value = list1[i];
        ddlProvince.appendChild(option);
        //city initialize
    }
     for (var j = 0; j < firstprovince.length; j++) {
            var optioncity = document.createElement("option");
            optioncity.appendChild(document.createTextNode(firstprovince[j]));
            optioncity.value = firstprovince[j];
            ddlCity.appendChild(optioncity);
        }
    
    function indexof(obj,value)
    {
        var k=0;
        for(;k<obj.length;k++)
        {
            if(obj[k] == value)
            return k;
        }
        return k;
    }
    function selectprovince(obj) {
        ddlCity.options.length = 0;//clear
        var index = indexof(list1,obj.value);
        var list2element = list2[index];
        for(var i =0;i<list2element.length; i++)
        {
            var option = document.createElement("option");
            option.appendChild(document.createTextNode(list2element[i]));
            option.value = list2element[i];
            ddlCity.appendChild(option);
        }
    }
</script>
</html>